﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using TomShane.Neoforce.Controls;

namespace SpaceInvade
{
    // Game Main Menu Holds the game setup options and high score
    public class StartMenu : GameState
    {
        Button btnStart = null;
        ImageBox ibxStars = null;

        SideBar pnlSideBar = null;

        SideBarPanel sbpModes = null;
        Button btnModeButton = null;
        List<string> strModes = new List<string>() {"Normal Mode", "Free Mode", "Invasion Mode", "Marathon Mode" };
        List<Button> lstModeButtons = new List<Button>();
        string strSelectedMode = null;
        Label lblSelectedMode = null;

        SideBarPanel sbpDifficulty = null;
        Label lblEnemyDifficulty = null;
        TrackBar tkbEnemyDifficulty = null;
        Label lblItemDifficulty = null;
        TrackBar tkbItemDifficulty = null;

        List<SideBar> lstPlayerPanels = new List<SideBar>();
        List<Button> lstAddPlayerButton = new List<Button>();

        GameInfo NewGameInfo = new GameInfo();

        List<string> strControls = new List<string>() { "Left", "Right", "Up", "Down", "Shoot", "Special" };
        List<Button> lstControlButtons = new List<Button>();
        List<ImageBox> lstShipIcons = new List<ImageBox>();
        bool PollKeyboard = false;
        int SelectedControlButton = 0;
        int SelectedControlPlayer = 0;

        private Texture2D StarScape = null;
        SpriteBatch spriteBatch = null;
        Rectangle StarsSrc;
        Rectangle StarsDest;

        Label lblDisposable = null;

        public StartMenu(Manager manager) : base (manager)
        {
            StarScape = Manager.Content.Load<Texture2D>(@"Images\StarScape");
            PlayerInfo.lstShipIcons.Add(Manager.Content.Load<Texture2D>(@"Textures\playerShip"));
            PlayerInfo.lstShipIcons.Add(Manager.Content.Load<Texture2D>(@"Textures\playerShip2"));
            spriteBatch = new SpriteBatch(Manager.GraphicsDevice);
            StarsSrc = new Rectangle(0, 0, Manager.Graphics.PreferredBackBufferWidth, Manager.Graphics.PreferredBackBufferHeight);
            StarsDest = new Rectangle(0, 0, Manager.Graphics.PreferredBackBufferWidth, Manager.Graphics.PreferredBackBufferHeight);
            Sound.PlaySong("sngMenu");
            for (int x = 0; x < 4; x++)
            {
                NewGameInfo.lstPlayers[x].ShipIcon = PlayerInfo.lstShipIcons[0];
            }
            InitGUI();
        }
        public override void Update()
        {
            lblDisposable.Text = lstControlButtons.Count.ToString();//Disposable.Count.ToString();
            
            if (PollKeyboard)
            {
                KeyboardState pressedKey = Keyboard.GetState();
                Keys[] allKeys = pressedKey.GetPressedKeys();
                if (allKeys.Length == 1)
                {
                    int Selection = SelectedControlButton + SelectedControlPlayer * 6;
                    NewGameInfo.lstPlayers[SelectedControlPlayer].ControlKeys[SelectedControlButton] = allKeys[0];
                    lstControlButtons[Selection].Text = allKeys[0].ToString();
                    lstControlButtons[Selection].Color = Color.Silver;
                    PollKeyboard = false;
                    SelectedControlButton = 0;
                    SelectedControlPlayer = 0;
                }
            }
        }
        //Draw Gui 
        public override void Draw()
        {
        }
        public override void InitializeCurrentLevel(int CurrentLevel)
        {
            //Load the next level and initialize it
        }
        private void InitGUI()
        {

            ibxStars = new ImageBox(Manager);
            ibxStars.Init();
            ibxStars.Top = 0;
            ibxStars.Left = 0;
            ibxStars.Height = Manager.Graphics.PreferredBackBufferHeight;
            ibxStars.Width = Manager.Graphics.PreferredBackBufferWidth;
            ibxStars.Image = StarScape;
            Manager.Add(ibxStars);

            pnlSideBar = new SideBar(Manager);
            pnlSideBar.Init();
            pnlSideBar.Parent = ibxStars;
            pnlSideBar.Anchor = Anchors.Left | Anchors.Top | Anchors.Bottom;
            pnlSideBar.Width = 140;
            pnlSideBar.Height = ibxStars.Height;

            btnStart = new Button(Manager);
            btnStart.Init();
            btnStart.Parent = pnlSideBar;
            btnStart.Top = Manager.Graphics.PreferredBackBufferHeight - 50;
            btnStart.Left = 10;
            btnStart.Width = 100;
            btnStart.Text = "Start Game";
            btnStart.CanFocus = false;
            btnStart.Click +=new TomShane.Neoforce.Controls.EventHandler(btnStart_Click);

            lblDisposable = new Label(Manager);
            lblDisposable.Init();
            lblDisposable.Parent = ibxStars;
            lblDisposable.Top = 20;
            lblDisposable.Left = lblDisposable.Parent.Width - 50;
            lblDisposable.Text = Disposable.Count.ToString();
            lblDisposable.Width = 50;
            
            InitModeBar();
            InitDiffBar();
            InitPlayerPanel();

        }
        public void InitModeBar()
        {
            sbpModes = new SideBarPanel(Manager);
            sbpModes.Init();
            sbpModes.Parent = pnlSideBar;
            sbpModes.Top = 20;
            sbpModes.Left = 10;
            sbpModes.Width = 130;
            sbpModes.Height = 160;

            lblSelectedMode = new Label(Manager);
            lblSelectedMode.Init();
            lblSelectedMode.Parent = sbpModes;
            lblSelectedMode.Top = 10;
            lblSelectedMode.Left = 10;
            lblSelectedMode.Text = "Mode not chosen";
            lblSelectedMode.Width = 100;

            int x = 40;
            foreach (string modename in strModes)
            {
                btnModeButton = new Button(Manager);
                btnModeButton.Init();
                btnModeButton.Parent = sbpModes;
                btnModeButton.Name = modename;
                btnModeButton.Text = modename;
                btnModeButton.Top = x;
                btnModeButton.Left = 10;
                btnModeButton.Width = 100;
                btnModeButton.Click += new TomShane.Neoforce.Controls.EventHandler(btnMode_Click);
                lstModeButtons.Add(btnModeButton);
                x += 30;
            }

        }

        private void InitDiffBar()
        {
            sbpDifficulty = new SideBarPanel(Manager);
            sbpDifficulty.Init();
            sbpDifficulty.Parent = pnlSideBar;
            sbpDifficulty.Top = 200;
            sbpDifficulty.Left = 10;
            sbpDifficulty.Width = 130;
            sbpDifficulty.Height = 100;

            lblEnemyDifficulty = new Label(Manager);
            lblEnemyDifficulty.Init();
            lblEnemyDifficulty.Parent = sbpDifficulty;
            lblEnemyDifficulty.Top = 10;
            lblEnemyDifficulty.Left = 10;
            lblEnemyDifficulty.Text = "Enemy Difficulty:";
            lblEnemyDifficulty.Width = 100;

            tkbEnemyDifficulty = new TrackBar(Manager);
            tkbEnemyDifficulty.Init();
            tkbEnemyDifficulty.Parent = sbpDifficulty;
            tkbEnemyDifficulty.Top = 30;
            tkbEnemyDifficulty.Left = 10;
            tkbEnemyDifficulty.Width = 100;
            tkbEnemyDifficulty.Range = 100;
            tkbEnemyDifficulty.ValueChanged += new TomShane.Neoforce.Controls.EventHandler(tkbDifficulty_ValueChanged);

            lblItemDifficulty = new Label(Manager);
            lblItemDifficulty.Init();
            lblItemDifficulty.Parent = sbpDifficulty;
            lblItemDifficulty.Top = 50;
            lblItemDifficulty.Left = 10;
            lblItemDifficulty.Text = "Item Difficulty:";
            lblItemDifficulty.Width = 100;

            tkbItemDifficulty = new TrackBar(Manager);
            tkbItemDifficulty.Init();
            tkbItemDifficulty.Parent = sbpDifficulty;
            tkbItemDifficulty.Top = 70;
            tkbItemDifficulty.Left = 10;
            tkbItemDifficulty.Width = 100;
            tkbItemDifficulty.Range = 100;
            tkbItemDifficulty.ValueChanged += new TomShane.Neoforce.Controls.EventHandler(tkbDifficulty_ValueChanged);
        }

        private void btnStart_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            //Dispose();
            switch(strSelectedMode)
            {
                case "Free Mode":StateManager.SelectMode(new FreeMode(Manager, NewGameInfo)); break;
                case "Normal Mode":StateManager.SelectMode(new NormalMode(Manager)); break;
                case "Marathon Mode":StateManager.SelectMode(new MarathonMode(Manager)); break;
                case "Invasion Mode": StateManager.SelectMode(new InvasionMode(Manager)); break;
                default: break;
            }
        }

        public void btnMode_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            strSelectedMode = (sender as Button).Text;
            lblSelectedMode.Text = strSelectedMode;
        }

        private void tkbDifficulty_ValueChanged(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            if ((sender as TrackBar).Value >= 0 && (sender as TrackBar).Value <= (sender as TrackBar).Range / 8)
            {
                (sender as TrackBar).Value = 0;
            }
            else if ((sender as TrackBar).Value >= (sender as TrackBar).Range / 8 && (sender as TrackBar).Value <= (sender as TrackBar).Range / 3)
            {
                (sender as TrackBar).Value = (sender as TrackBar).Range / 4;
            }
            else if ((sender as TrackBar).Value >= (sender as TrackBar).Range / 3 && (sender as TrackBar).Value <= (float)(sender as TrackBar).Range * 0.66f)
            {
                (sender as TrackBar).Value = (sender as TrackBar).Range / 2;
            }
            else if ((sender as TrackBar).Value >= (float)(sender as TrackBar).Range * 0.66f && (sender as TrackBar).Value <= (float)(sender as TrackBar).Range * 0.85f)
            {
                (sender as TrackBar).Value = (int)((float)(sender as TrackBar).Range * 0.75f);
            }
            else
            {
                (sender as TrackBar).Value = (sender as TrackBar).Range;
            }
        }

        private void InitPlayerPanel()
        {
            for (int x = 0; x < 4; x++)
            {
                SideBar pnlPlayer = new SideBar(Manager);
                pnlPlayer.Init();
                pnlPlayer.Parent = ibxStars;
                pnlPlayer.Name = "pnlPlayer" + x.ToString();
                pnlPlayer.Top = 70 + ((lstPlayerPanels.Count - lstPlayerPanels.Count % 2) * 105);
                pnlPlayer.Left = ((lstPlayerPanels.Count % 2) * 220) + 190;
                pnlPlayer.Width = 180;
                pnlPlayer.Height = 180;

                lstPlayerPanels.Add(pnlPlayer);

                DetailPlayerPanel(x);
                if (x > 0)
                {
                    Button btnAddPlayer = new Button(Manager);
                    btnAddPlayer.Init();
                    btnAddPlayer.Parent = pnlPlayer;
                    btnAddPlayer.Name = "btnPlayerAdd" + x.ToString();
                    btnAddPlayer.Text = "Add Player";
                    btnAddPlayer.Top = btnAddPlayer.Parent.ClientHeight - 30;
                    btnAddPlayer.Left = btnAddPlayer.Parent.ClientWidth / 2 - 40;
                    btnAddPlayer.Click += new TomShane.Neoforce.Controls.EventHandler(btnAddPlayer_Click);

                    lstAddPlayerButton.Add(btnAddPlayer);

                    HidePlayerPanel(x);
                }
            }
        }

        private void btnAddPlayer_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            int x = 1;
            foreach (Button b in lstAddPlayerButton)
            {
                if ((sender as Button).Name == "btnPlayerAdd" + x.ToString())
                {
                    if ((sender as Button).Text == "Remove")
                    {
                        HidePlayerPanel(x);
                        NewGameInfo.lstPlayers[x].ClearKeys();
                        for(int bu = 0; bu < 6; bu++)
                            lstControlButtons[(x-1) * 6 + bu].Text = strControls[bu];
                        (sender as Button).Text = "Add Player";
                    }
                    else
                    {
                        RevealPlayerPanel(x);
                        (sender as Button).Text = "Remove";
                    }

                    break;
                }
                x++;
            }
        }

        private void DetailPlayerPanel(int Plr)
        {
            SideBar pnlPlayerDetails = new SideBar(Manager);
            pnlPlayerDetails.Init();
            pnlPlayerDetails.Parent = lstPlayerPanels[Plr];
            pnlPlayerDetails.Name = "pnlPlayerDetails" + Plr.ToString();
            pnlPlayerDetails.Top = 0;
            pnlPlayerDetails.Left = 0;
            pnlPlayerDetails.Width = pnlPlayerDetails.Parent.Width;
            pnlPlayerDetails.Height = pnlPlayerDetails.Parent.Height - 30;
            pnlPlayerDetails.Color = Color.Transparent;

            ImageBox ibxShipIcon = new ImageBox(Manager);
            ibxShipIcon.Init();
            ibxShipIcon.Parent = pnlPlayerDetails;
            ibxShipIcon.Name = "ibxShipA" + Plr.ToString();
            ibxShipIcon.Image = PlayerInfo.lstShipIcons[0];
            ibxShipIcon.Top = 40;
            ibxShipIcon.Left = 20;
            ibxShipIcon.Height = 36;
            ibxShipIcon.Width = 36;
            ibxShipIcon.Passive = true;
            lstShipIcons.Add(ibxShipIcon);

            Button btnScrollShipLeft = new Button(Manager);
            btnScrollShipLeft.Init();
            btnScrollShipLeft.Parent = pnlPlayerDetails;
            btnScrollShipLeft.Name = "ScrollShipLeft" + Plr.ToString();
            btnScrollShipLeft.Top = 50;
            btnScrollShipLeft.Left = 5;
            btnScrollShipLeft.Width = 15;
            btnScrollShipLeft.Height = 15;
            btnScrollShipLeft.Color = Color.SkyBlue;
            btnScrollShipLeft.Text = "<";
            btnScrollShipLeft.Click += new TomShane.Neoforce.Controls.EventHandler(btnScrollShipLeft_Click);

            Button btnScrollShipRight = new Button(Manager);
            btnScrollShipRight.Init();
            btnScrollShipRight.Parent = pnlPlayerDetails;
            btnScrollShipRight.Name = "ScrollShipRight" + Plr.ToString();
            btnScrollShipRight.Top = 50;
            btnScrollShipRight.Left = 53;
            btnScrollShipRight.Width = 15;
            btnScrollShipRight.Height = 15;
            btnScrollShipRight.Color = Color.SkyBlue;
            btnScrollShipRight.Text = ">";
            btnScrollShipRight.Click += new TomShane.Neoforce.Controls.EventHandler(btnScrollShipRight_Click);

            SideBar pnlControls = new SideBar(Manager);
            pnlControls.Init();
            pnlControls.Parent = pnlPlayerDetails;
            pnlControls.Top = 10;
            pnlControls.Left = pnlControls.Parent.Width - 100;
            pnlControls.Height = 135;
            pnlControls.Width = 100;
            

            int Y = 0;
            foreach (string s in strControls)
            {
                Label lblControl = new Label(Manager);
                lblControl.Init();
                lblControl.Parent = pnlControls;
                lblControl.Top = 10 + Y * 20;
                lblControl.Left = 55;
                lblControl.Text = strControls[Y];

                Button btnControl = new Button(Manager);
                btnControl.Init();
                btnControl.Name = strControls[Y] + Plr.ToString();
                btnControl.Parent = pnlControls;
                btnControl.Top = 10 + Y * 20;
                btnControl.Left = 0;
                btnControl.Text = strControls[Y];
                btnControl.Height = 20;
                btnControl.Width = 50;
                btnControl.Color = Color.Silver;
                btnControl.Click += new TomShane.Neoforce.Controls.EventHandler(btnControl_Click);
                lstControlButtons.Add(btnControl);

                Y++;
            }
        }

        private void HidePlayerPanel(int x)
        {
            lstPlayerPanels[x].GetControl("pnlPlayerDetails" + x.ToString()).Visible = false;
            NewGameInfo.lstPlayers[x].InUse = false;
        }

        private void RevealPlayerPanel(int x)
        {
            lstPlayerPanels[x].GetControl("pnlPlayerDetails" + x.ToString()).Visible = true;
            NewGameInfo.lstPlayers[x].InUse = true;
        }
        
        private void btnControl_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            int Selection = 0;
            foreach (Button b in lstControlButtons)
            {
                if (b.Name == (sender as Button).Name)
                {
                    SelectedControlPlayer = Selection / 6;
                    SelectedControlButton = Selection % 6;
                    PollKeyboard = true;
                    foreach (Button bu in lstControlButtons)
                        bu.Color = Color.Silver;
                    (sender as Button).Color = Color.Green;
                    break;
                }
                Selection++;
            }
        }

        public override void Dispose()
        {
            ibxStars.Dispose();
            StarScape.Dispose();
            for (int x = Manager.Components.Count() - 1; x > 0; x--)
                Manager.Remove(Manager.Components.ElementAt(x));
        }

        private void btnScrollShipLeft_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            for (int x = 0; x < 4; x++)
            {
                if ((sender as Button).Name == "ScrollShipLeft" + x.ToString())
                {
                    NewGameInfo.lstPlayers[x].ShipType--;
                    lstShipIcons[x].Image = NewGameInfo.lstPlayers[x].ShipIcon;
                    break;
                }
            }
        }

        private void btnScrollShipRight_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            for (int x = 0; x < 4; x++)
            {
                if ((sender as Button).Name == "ScrollShipRight" + x.ToString())
                {
                    NewGameInfo.lstPlayers[x].ShipType++;
                    lstShipIcons[x].Image = NewGameInfo.lstPlayers[x].ShipIcon;
                    break;
                }
            }
        }
    }
}
